﻿Public Class VM_BranchReport
	Implements IDisposable

	Dim db As New CongregationDataContainerLocal

	Dim myReportingPublishers As List(Of PublisherCardData)
	Dim myReportingAuxPioneers As List(Of PublisherCardData)
	Dim myReportingPioneers As List(Of PublisherCardData)

	Public ReadOnly Property ActivePublisherCount As Integer
		Get
			Return PublisherData.ActivePublishers.Count
		End Get
	End Property

	Public ReadOnly Property PublisherCount As Integer
		Get
			Return myReportingPublishers.Count
		End Get
	End Property

	Public ReadOnly Property PublisherBooks As Integer
		Get
			If PublisherCount > 0 Then
				Return myReportingPublishers.Sum(Function(x) x.Books)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PublisherBrochures As Integer
		Get
			If PublisherCount > 0 Then
				Return myReportingPublishers.Sum(Function(x) x.Brochures)
			Else
				Return 0
			End If
		End Get
	End Property

	'fractional hours must be handled on a per-publisher basis
	Public ReadOnly Property PublisherHours As Integer
		Get
			If PublisherCount > 0 Then
				'Dim db As New CongregationDataContainerLocal
				Dim totalHours As Integer

				Dim myPublishersTime = From pub As PublisherData In PublisherData._ReportingPublishers
				   Group Join rpt As PublisherCardData In db.PublisherCardDatas.Where(Function(x) Not x.IsHistorical AndAlso Not x.IsAuxPioneer AndAlso Not x.IsPioneer AndAlso Not x.IsSpecialPioneer)
				   On pub.PublisherID Equals rpt.PublisherID
				   Into LoggedTime = Group
				   Select pub,
				   ReportedTime = LoggedTime.Where(Function(x) x.Reported).Sum(Function(x) x.Hours),
				 UnreportedTime = LoggedTime.Where(Function(x) Not x.Reported).Sum(Function(x) x.Hours)

				For Each pub In myPublishersTime
					If Decimal.Truncate(pub.ReportedTime) = pub.ReportedTime Then
						'we are working with integers here, so there is nothing to carry over
						totalHours += Decimal.Truncate(pub.UnreportedTime)
						Diagnostics.Debug.WriteLine(pub.pub.FullName & " - " & pub.UnreportedTime)
					Else
						'add the unreported time to the fractional hours for this reporting period
						totalHours += Decimal.Truncate(pub.UnreportedTime + pub.ReportedTime - Decimal.Truncate(pub.ReportedTime))
					End If
				Next

				Return totalHours
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PublisherMagazines As Integer
		Get
			If PublisherCount > 0 Then
				Return myReportingPublishers.Sum(Function(x) x.Magazines)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PublisherReturnVisits As Integer
		Get
			If PublisherCount > 0 Then
				Return myReportingPublishers.Sum(Function(x) x.ReturnVisits)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PublisherStudies As Integer
		Get
			If PublisherCount > 0 Then
				Return myReportingPublishers.Sum(Function(x) x.BibleStudies)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerCount As Integer
		Get
			Return myReportingAuxPioneers.Count
		End Get
	End Property

	Public ReadOnly Property AuxPioneerBooks As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.Books)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerBrochures As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.Brochures)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerHours As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.Hours)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerMagazines As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.Magazines)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerReturnVisits As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.ReturnVisits)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property AuxPioneerStudies As Integer
		Get
			If AuxPioneerCount > 0 Then
				Return myReportingAuxPioneers.Sum(Function(x) x.BibleStudies)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerCount As Integer
		Get
			Return myReportingPioneers.Count
		End Get
	End Property

	Public ReadOnly Property PioneerBooks As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.Books)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerBrochures As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.Brochures)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerHours As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.Hours)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerMagazines As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.Magazines)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerReturnVisits As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.ReturnVisits)
			Else
				Return 0
			End If
		End Get
	End Property

	Public ReadOnly Property PioneerStudies As Integer
		Get
			If PioneerCount > 0 Then
				Return myReportingPioneers.Sum(Function(x) x.BibleStudies)
			Else
				Return 0
			End If
		End Get
	End Property

	Public Sub New()
		myReportingPublishers = PublisherCardData.ReportingPublishersReports()
		myReportingAuxPioneers = PublisherCardData.ReportingAuxPioneersReports()
		myReportingPioneers = PublisherCardData.ReportingPioneersReports()

	End Sub

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
